Skip to main content
Creates and configures a contrastive vision-language model with flexible weight loading options.

Signature

Parameters

str
required
Model identifier, potentially with schema prefix:
  • 'ViT-B-32': Built-in model name. pretrained specifies CLIP weights source (tag or file path).
  • 'hf-hub:org/repo': Loads config/weights from HuggingFace Hub. pretrained is IGNORED.
  • 'local-dir:/path/to/folder': Loads config/weights from local directory. pretrained is IGNORED.
Optional[str]
default:"None"
Source for CLIP weights (tag or file path) ONLY if model_name has no schema. Can be a pretrained tag like 'openai', 'laion400m_e32', or a path to a checkpoint file.
bool
default:"True"
Load the resolved pretrained weights if True, otherwise random init or tower overrides only.
str
default:"'fp32'"
Model precision. Options: 'fp32', 'fp16', 'bf16', 'pure_fp16', 'pure_bf16'.
Union[str, torch.device]
default:"'cpu'"
Device to load model on. Can be 'cpu', 'cuda', or a torch.device object.
bool
default:"False"
If True, JIT compile the model using torch.jit.script.
bool
default:"False"
Force use of QuickGELU activation in model config instead of standard GELU.
bool
default:"False"
Force use of custom text encoder architecture (CustomTextCLIP).
Optional[float]
default:"None"
Override patch dropout value in model config. Values typically range from 0.0 to 1.0.
Optional[Union[int, Tuple[int, int]]]
default:"None"
Override image size in model config. Can be a single int (square) or tuple (height, width).
Optional[Dict[str, Any]]
default:"None"
Dictionary to override specific preprocessing parameters (mean, std, interpolation, resize_mode).
Optional[int]
default:"None"
Override context length (max sequence length) in text config.
bool
default:"False"
Load default base weights for image tower at creation if no CLIP weights loaded. Only effective for timm-based vision models.
bool
default:"True"
Load default base weights for text tower at creation if no CLIP weights loaded. Only effective for HuggingFace-based text models.
Optional[str]
default:"None"
Path to load weights specifically into image tower after model creation. Loads after full CLIP checkpoint.
Optional[str]
default:"None"
Path to load weights specifically into text tower after model creation. Loads after full CLIP checkpoint.
Optional[str]
default:"None"
Cache directory for downloaded weights. Defaults to ~/.cache/clip.
Optional[bool]
default:"None"
If True and model supports it, return dictionary output instead of tensors.
bool
default:"False"
Raise error if no pretrained CLIP weights loaded when required.
bool
default:"True"
Use weights_only=True for torch.load (safer, prevents arbitrary code execution).
Any
Additional keyword arguments for model constructor (highest override priority).

Returns

torch.nn.Module
The created model instance (CLIP, CustomTextCLIP, or CoCa depending on configuration).

Example